import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 			= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 		= $Lazy.file(()=>import('#private/cache-api'))
const $time 		= $Lazy.file(()=>import('@com/utils/time'))
const UpdataModal 	= $Lazy.state(()=>import('./cpt/updata-modal'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		// 搜索表单列表
		forms : [
			{ label: '标本条码',		name: 'spec_code' },
			{ label: '实验号',		name: 'experiment_num' },
			{ label: '项目名称',		name: 'kind_id',	type: 'select', loading:true },
			{ label: '通道号',		name: 'device_channel' },
			{ label: '结果时间',		names: ['start_at','end_at'],	name: 'jz-time',	type:'date-range' },
			{ label: '设备编号',		name: 'device_uid' },
			{ label: '设备名称',		name: 'device_name',	type: 'select', loading:true, idStr: 'value', nameStr: 'name' },
		],
		updateData:[]
	}
	
	model = {}
	
	componentDidMount(){
		this.getSelect()
		// 设置时间默认值
		$time.then(f=>{
			const arr = f.duration['1-today']()
			this.resetModel = { start_at: arr[0], end_at: arr[1], 'jz-time':arr }
			this.model = $fn.copy(this.resetModel)
			if(window.isInit){
				this.fetch()
			}
		})
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		
		$cache.then( async f => {
			// 项目名称 
			$fn.setRow(forms, 'kind_id', await f.getCache({ api:'project_name' } ))
			// 设备名称
			$fn.setRow(forms, 'device_name', await f.getCache({ api:'device_result', way:'post' } ))
			//
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'result-unit-item/getDeviceResultList', { isIndex:true })
	
	// 表单
	cols = [
		{ title: '标本条码',		dataIndex: 'spec_code', 		width:150 },
		{ title: '实验号', 		dataIndex: 'lab_tag', 			width:150 },
		{ title: '通道号', 		dataIndex: 'device_channel', 	width:150 },
		{ title: '结果时间', 	dataIndex: 'msg_send_at', 		width:150, align:'center' },
		{ title: '设备编号', 	dataIndex: 'device_uid', 		width:150 },
		{ title: '设备名称', 	dataIndex: 'device_name', 		width:150 },
		{ title: '项目', 		dataIndex: 'kind_name', 		width:150 },
		{ title: '值', 			dataIndex: 'value', 			width:50 },
		{ title: '值1', 		dataIndex: 'value1', 			width:50 },
		{ title: '值2', 		dataIndex: 'value2', 			width:50 },
		{ title: '值3', 		dataIndex: 'value3', 			width:50 },
	]
	// 操作按钮
	controls = () => [
		Controls.click.call(this,'手动更新', ()=> this.updataModalRef.open())
	]

	render(){
		return (
			<>
				<Page title='设备结果管理' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this, { timeStr:'1-today' }) }
					{/* 表格 */}
					{ Table.call(this, { isIndex:true, isInit:window.isInit }) }
				</Page>
				{/* 更新 */}
				<UpdataModal cols={this.cols} onRef={ref=>this.updataModalRef=ref} />
			</>
		)
	}
}

export default Index